{% extends 'layout/basic.html' %}
{% load static %}

{% block title %}项目管理-注册页面{% endblock %}

{% block css %}
    <link rel="stylesheet" href="{% static 'css/account.css' %}">
    <style>
        .error-msg {
            color: red;
            position: absolute;
        }
    </style>
{% endblock %}

{% block content %}
    <div class="account">
        <div class="title">用户注册</div>
        <form id="regForm" method="POST" novalidate>
            {% csrf_token %}
            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        <div class="row">
                            <div class="col-xs-7">
                                {{ field }}
                                <span class="error-msg"></span>
                            </div>
                            <div class="col-xs-5">
                                <input id="btnSms" type="button" class="btn btn-default" value="点击获取验证码">
                            </div>
                        </div>
                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {{ field }}
                        <span class="error-msg"></span>
                    </div>
                {% endif %}
            {% endfor %}

            <div class="row">
                <div class="col-xs-3">
                    <input id="btnSubmit" type="button" class="btn btn-primary" value="注  册"/>
                </div>
            </div>
        </form>
    </div>
{% endblock %}

{% block js %}
    <script type="text/javascript">
        //页面框架加载完成之后自动执行的函数
        $(function () {
            bindBtnSmsClickEvent();
            bindBtnSubmitClickEvent();
        });

        /*
            发送短信验证码函数
         */
        function bindBtnSmsClickEvent() {
            $("#btnSms").click(function () {
                    $(".error-msg").empty();
                    //获取手机号
                    var mobilePhone = $("#id_mobile_phone").val();
                    //发送AJAX请求
                    $.ajax({
                        url: "{% url 'web:send_sms' %}",
                        type: "POST",
                        data: {mobile_phone: mobilePhone, frl: "register"},
                        success: function (res) {
                            if (res.status) {
                                var $smsBtn = $("#btnSms")
                                $smsBtn.prop('disabled', true);
                                var time = res.count_down_seconds;
                                var obj = setInterval(function () {
                                    $smsBtn.val(time + "秒后重新发送")
                                    time = time - 1;
                                    if (time < 1) {
                                        $smsBtn.prop('disabled', false);
                                        $smsBtn.val("请点击重新发送")
                                        clearInterval(obj);
                                    }
                                }, 1000);
                            } else {
                                $.each(res.errors, function (k, v) {
                                        $("#id_" + k).next().text(v[0]);
                                    }
                                )
                            }
                        }
                    })
                }
            )
        }

        function bindBtnSubmitClickEvent() {
            $('#btnSubmit').click(function () {
                $('.error-msg').empty();
                $.ajax({
                    url: "{% url 'web:register_submit' %}",
                    type: 'post',
                    dataType: 'JSON',
                    data: $('#regForm').serialize(),
                    success: function (res) {
                        if (res.status) {
                            location.href = res.data
                        } else {
                            $.each(res.errors, function (k, v) {
                                $('#id_' + k).next().text(v)
                            })
                        }
                    }
                })
            })
        }

    </script>
{% endblock %}